#include "wordlegame.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QDebug>
#include <QTime>
#include<QApplication>

WordleGame::WordleGame(QWidget *parent) : QWidget(parent)
{
    // Initialize UI elements
    QLabel *titleLabel = new QLabel("Welcome to Wordle!");
    titleLabel->setAlignment(Qt::AlignCenter);

    guessLineEdit = new QLineEdit;
    guessLineEdit->setPlaceholderText("Enter your guess");
    guessLineEdit->setMaxLength(5); // Assuming the word length is 5 characters
    guessLineEdit->setAlignment(Qt::AlignCenter);

    checkButton = new QPushButton("Check");
    connect(checkButton, &QPushButton::clicked, this, &WordleGame::checkGuess);

    playAgainButton = new QPushButton("Play Again");
    connect(playAgainButton, &QPushButton::clicked, this, &WordleGame::playAgain);

    quitButton = new QPushButton("Quit");
    connect(quitButton, &QPushButton::clicked, this, &WordleGame::quitGame);

    resultLabel = new QLabel;
    resultLabel->setAlignment(Qt::AlignCenter);

    guessesLeftLabel = new QLabel;
    guessesLeftLabel->setAlignment(Qt::AlignCenter);

    // Layout setup
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(titleLabel);
    mainLayout->addWidget(guessLineEdit);
    mainLayout->addWidget(checkButton);
    mainLayout->addWidget(playAgainButton);
    mainLayout->addWidget(quitButton);
    mainLayout->addWidget(resultLabel);
    mainLayout->addWidget(guessesLeftLabel);

    setLayout(mainLayout);

    // Initialize the game
    srand(QTime::currentTime().msec());
    startNewGame();
}

void WordleGame::startNewGame()
{
    // Reset guesses left
    guessesLeft = 6;

    // Select a random word from the word list
    wordList << "match""apple"<<"sunny"<<
        "melon"<<"ocean"<<
        "happy"<<"chill"<<
        "frost"<<"queen"<<
        "magic"<<"music"<<
        "dream"<<"party"<<
        "piano"<<"light"<<
        "sweet"<<"tulip"<<
        "rocks"<<"vivid"<<
        "smile"<<"fairy"<<
        "water"<<"giant"<<
        "books"<<"daisy"<<
        "stars"<<"night"<<
        "green"<<"globe"<<
        "grass"<<"peace"<<
        "puppy"<<"happy"<<
        "ocean"<<"waves"<<
        "quiet"<<"watch"<<
        "flame"<<"dance"<<
        "feast"<<"earth"<<
        "river"<<"swift"<<
        "light"<<"torch"<<
        "music"<<"notes"<<
        "dance"<<"grace"<<
        "fresh"<<"lemon"<<
        "brave"<<"heart"<<"sweet"<<"dream"<<
        "brave"<<"candy"<<
        "grace"<<"light"<<
        "music"<<"heart"<<
        "angel"<<"peace"<<
        "stars"<<"night"<<
        "river"<<"swift"<<
        "heart"<<"faith"; // Add more words if needed
    int wordIndex = rand() % wordList.size();
    targetWord = wordList.at(wordIndex);

    // Hide the target word
    hiddenTargetWord = targetWord.at(0);
    for (int i = 1; i < targetWord.length(); ++i) {
        hiddenTargetWord += '-';
    }

    // Update UI
    resultLabel->setText("");
    guessesLeftLabel->setText("Guesses left: " + QString::number(guessesLeft));
    guessLineEdit->setEnabled(true);
    checkButton->setEnabled(true);
    guessLineEdit->clear();
    guessLineEdit->setFocus();
}

void WordleGame::checkGuess()
{
    QString guess = guessLineEdit->text().trimmed().toLower();
    if (guess.length() != 5) {
        QMessageBox::warning(this, "Invalid Guess", "Please enter a 5-letter word.");
        return;
    }

    // Check if the guess is correct
    if (guess == targetWord) {
        resultLabel->setText("Congratulations! You guessed the word: " + targetWord);
        guessLineEdit->setEnabled(false);
        checkButton->setEnabled(false);
        return;
    }

    // Check if the guess is partially correct
    QString feedback = "";
    for (int i = 0; i < targetWord.length(); ++i) {
        if (guess.at(i) == targetWord.at(i)) {
            feedback += guess.at(i);
        } else if (targetWord.contains(guess.at(i)) && !isLetterRepeated(guess, guess.at(i))) {
            feedback += "X";
        } else {
            feedback += "-";
        }
    }

    // Update UI
    resultLabel->setText("Incorrect. Feedback: " + feedback);
    guessesLeft--;
    guessesLeftLabel->setText("Guesses left: " + QString::number(guessesLeft));

    // Check if the player has run out of guesses
    if (guessesLeft == 0) {
        resultLabel->setText("Sorry, you've run out of guesses. The word was: " + targetWord);
        guessLineEdit->setEnabled(false);
        checkButton->setEnabled(false);
    }
}

void WordleGame::playAgain()
{
    startNewGame();
}

void WordleGame::quitGame()
{
    QApplication::quit();
}

bool WordleGame::isLetterRepeated(const QString& word, QChar letter)
{
    int count = 0;
    for (int i = 0; i < word.length(); ++i) {
        if (word.at(i) == letter) {
            count++;
        }
    }
    return count > 1;
}
